package cn.jc.spider.neww.storer;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.torque.util.Criteria;

import cn.jc.spider.po.Unvisitedurl;


/**
 * 
 * StorerI目前能想到的有回调请求一个入库的接口方式(JSON，XML，或其它)，直接连接数据库入库方式(本地和远程)，输出到某一个输出流一种方式(本地文本等)
 * 
 * @author JiangChi
 *
 */
public abstract class StorerImpl implements StorerI {

	@Override
	public void runsaveinfo(Unvisitedurl url, Map<String, Object> jiexicontent) {
		if (MapUtils.isNotEmpty(jiexicontent)) {//当只有一个属性的时候，当作这个为list
			if (jiexicontent.size() == 1) {
				List<Map<String, Object>> list = (List<Map<String, Object>>) jiexicontent.get("list");
				for (Map<String, Object> m : list) {
					saveOneObject(m);
				}
			}
		}
	
	}

//	private void saveunvisiturl() {
//
//		String ssa = "";
//		String ssb = "";
//		String prefix = "";
//		for (Parser s : ps) {//原来是ss1
//			if (ssa.length() != 0 && ssb.length() != 0) {
//				prefix = ",";
//			}
//			ssa += prefix + "`" + s.getAttr() + "`";
//			ssb += prefix + "'" + m.get(s.getAttr()) + "'";
//		}
//		if (task.getIdNext() > 0) {
//			
//			if (psmap.get("fuzhubiaoshiid") != null) {
//				//这里只是特定的地方用到
//				Criteria cc = new Criteria();
//				cc.add(UnvisitedurlPeer.FUZHUBIAOSHIID, m.get("fuzhubiaoshiid"));
//				List<Unvisitedurl> uns = null;
//				try {
//					uns = UnvisitedurlPeer.doSelect(cc);
//				} catch (TorqueException e1) {
//				}
//				if (CollectionUtils.isNotEmpty(uns)) {
////			System.out.println("公司重复略过 公司名："+m.get("fuzhubiaoshiid")+" url:"+url.getUrl());
//					continue;
//				}
//			}
//			
//			String ss1 = "insert into unvisitedurl (`id_task`,`url`,`fuzhubiaoshiid`) values(" + task.getIdNext() + ",'" + ParserTools.fixUrl(url, (String)m.get("href")) + "','"+m.get("fuzhubiaoshiid")+"')";
//			try {
//				Criteria c = new Criteria();
//				c.add(UnvisitedurlPeer.URL, ParserTools.fixUrl(url, (String)m.get("href")));
//				List<Unvisitedurl> us = UnvisitedurlPeer.doSelect(c);
//				if (CollectionUtils.isEmpty(us)) {
//					BasePeer.executeStatement(ss1);
//				} else {
//					
//				}
//			} catch (TorqueException e) {
//				e.printStackTrace();
//				System.out.println("插入数据库异常 sql:" + ss1);
//			}
//		} else {
//			String ss = "insert into "+task.getName()+" (" + ssa + ") values(" + ssb + ")";
//			try {
//				BasePeer.executeQuery(ss);
//			} catch (TorqueException e) {
//				e.printStackTrace();
//				System.out.println("插入数据库异常 sql:" + ss);
//			}
//		}
//	
//	}

	public abstract void saveOneObject(Map<String, Object> m);
	protected static Criteria buildCriteria(String tbName,
			Map<String, Object> map) {
		Criteria criteria = new Criteria();
		for (String key : map.keySet()) {
			criteria.add(tbName + "." + key, map.get(key) + "");
		}
		return criteria;
	}
}
